home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / standards / sgml / nist / parse1 / dtdexit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-09-13  |  18.1 KB  |  502 lines

  1. /* National Institute of Standards and Technology (NIST)
  2. /* National Computer System Laboratory (NCSL)
  3. /* Office Systems Engineering (OSE) Group
  4. /* ********************************************************************
  5. /*                            D I S C L A I M E R
  6. /*                              (March 8, 1989)
  7. /*  
  8. /* There is no warranty for the NIST NCSL OSE SGML parser and/or the NIST
  9. /* NCSL OSE SGML parser validation suite.  If the SGML parser and/or
  10. /* validation suite is modified by someone else and passed on, NIST wants
  11. /* the parser's recipients to know that what they have is not what NIST
  12. /* distributed, so that any problems introduced by others will not
  13. /* reflect on our reputation.
  14. /* 
  15. /* Policies
  16. /* 
  17. /* 1. Anyone may copy and distribute verbatim copies of the SGML source
  18. /* code as received in any medium.
  19. /* 
  20. /* 2. Anyone may modify your copy or copies of SGML parser source code or
  21. /* any portion of it, and copy and distribute such modifications provided
  22. /* that all modifications are clearly associated with the entity that
  23. /* performs the modifications.
  24. /* 
  25. /* NO WARRANTY
  26. /* ===========
  27. /* 
  28. /* NIST PROVIDES ABSOLUTELY NO WARRANTY.  THE SGML PARSER AND VALIDATION
  29. /* SUITE ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
  30. /* EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  31. /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  32. /* THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS
  33. /* WITH YOU.  SHOULD THE SGML PARSER OR VALIDATION SUITE PROVE DEFECTIVE,
  34. /* YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
  35. /* 
  36. /* IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL NIST BE LIABLE FOR
  37. /* DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR OTHER SPECIAL,
  38. /* INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR
  39. /* INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA
  40. /* BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR A
  41. /* FAILURE OF THE PROGRAM TO OPERATE WITH PROGRAMS NOT DISTRIBUTED BY
  42. /* NIST) THE PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF
  43. /* SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  44. */
  45.  
  46. /************************************************************************/
  47. /*   TITLE:          SGML PARSER                                        */
  48. /*   SYSTEM:         DTD PROCESSOR                                      */
  49. /*   SUBSYSTEM:                                                         */
  50. /*   SOURCE FILE:    DTDEXIT.C                                          */
  51. /*   AUTHOR:         Jim Heath                                          */
  52. /*                                                                      */
  53. /*   DATE CREATED:                                                      */
  54. /*   LAST MODIFIED:                                                     */
  55. /*                                                                      */
  56. /*                  REVISIONS                                           */
  57. /*   WHEN      WHO            WHY                                       */
  58. /************************************************************************/
  59. #include <stdio.h>
  60. #include <setjmp.h>
  61. #include <fcntl.h>
  62. #include <unistd.h>
  63.  
  64. #include "qntyset.h"
  65. #include "dtd.h"
  66. #include "dtdglbl.h"
  67. #include "dtdfncs.h"
  68. #include "dtddefs.h"
  69.  
  70. /* ============================================================ */
  71. void bldsymbtbl()
  72. {
  73.    int temp, minrecs[MAXELTS], flag, rootgi = -1;
  74.    int count, j;
  75.    SYMBREC symbrec[MAXELTS];
  76.    extern char symbfname[];
  77.  
  78.    FUNCTRACE("bldsymbtbl");
  79.    symbfile = safeopen(symbfname, O_RDONLY, SYMBFILE);
  80.    for (count = 0; count < MAXELTS; count++)
  81.       memset((char *) &symbrec[count], ' ',sizeof(SYMBREC));
  82.    for (count = 0; count < MAXELTS; count++) {
  83.       temp = read(symbfile, (char *)&symbrec[count], sizeof(SYMBREC));
  84.       if (temp <= 0)
  85.          break;
  86.       if ((symbrec[count].Sname[0] & (1 << 7)) == (1 << 7)) {
  87.          symbrec[count].Sname[0] ^= (1 << 7);
  88.          rootgi = count;
  89.       }
  90.       for (j = 0, flag = FALSE; j < NAMELEN; j++){
  91.          if (symbrec[count].Sname[j] == '\0')
  92.             flag = TRUE;
  93.          if (flag == TRUE)
  94.             symbrec[count].Sname[j] = ' ';
  95.       }
  96.  
  97.       temp = read(symbfile, (char *)&minrecs[count], sizeof(int));
  98.       if (temp <= 0)
  99.          break;
  100.       symbrec[count].Smin = minrecs[count];
  101.    }
  102.    /* now check all the attributes */
  103.    safeclose(symbfile, "symbfile", SYMBFILE);
  104.    safeunlink(symbfname);
  105.    ckattributes(symbrec, count);
  106.    inclfile = safeopen(inclfname, O_RDONLY, INCLFILE);
  107.    xcptfile = safecreat(xcptfname, XCPTFILE);
  108.    doincludes(symbrec, count);
  109.    /*   safeunlink(inclfname, INCLFILE);*/
  110.    exclfile = safeopen(exclfname, O_RDONLY, EXCLFILE);
  111.    doexcludes(symbrec, count);
  112.    /*   safeunlink(exclfname, EXCLFILE);*/
  113.    safeclose(xcptfile, "xcptfile", XCPTFILE);
  114.    bldtokentree(symbrec, minrecs, count, rootgi);
  115. }
  116. /* ============================================================ */
  117. static void doincludes(srec, scount)
  118. SYMBREC srec[];
  119. int scount;
  120. {
  121.    REGISTER int j;
  122.    int count = 0, inclcount, elttoken, k;
  123.    char eltname[NAMELEN + 1], temp[128];
  124.    REGISTER char *eptr;
  125.    long position, lseek();
  126.  
  127.    safewrite(xcptfile, (char *) &count, sizeof(count));
  128.    for (count = 0; ; count++) {
  129.       if (read(inclfile, eltname, sizeof(eltname)) != sizeof(eltname)){
  130.          safeclose(inclfile, "inclfile", INCLFILE);
  131.          /* save current position */
  132.          if ((position = lseek(xcptfile, 0L, SEEK_CUR)) == -1)
  133.             terminate(1, "error on lseek on xcptfile");
  134.          /* position to beginning of file */
  135.          if (lseek(xcptfile, 0L, SEEK_SET) == -1)
  136.             terminate(1, "error on lseek on xcptfile");
  137.          safewrite(xcptfile, (char *) &count, sizeof(count));
  138.          /* restore pointer to current position */
  139.          if (lseek(xcptfile, position, SEEK_SET) == -1)
  140.             terminate(1, "error on lseek on xcptfile");
  141.          return;
  142.       }
  143.       for (j = 0, eptr = eltname; j < sizeof(eltname); j++, eptr++) {
  144.          if (*eptr == '\0')
  145.             *eptr = ' ';
  146.       }
  147.       if ((elttoken = find (eltname, srec, scount)) == -1) {
  148.          memset(temp, '\0', sizeof(temp));
  149.          strncpy(temp, eltname, sizeof(eltname));
  150.          strcat(temp, " is found in inclusion but no element declaration exists");
  151.          printf("%s\n", temp);
  152.       }
  153.       safewrite(xcptfile, (char *) &elttoken, sizeof(elttoken));
  154.  
  155.       if(read(inclfile, (char *) &inclcount, sizeof(inclcount)) != sizeof(inclcount))
  156.          terminate(1, "error in reading inclusion file");
  157.       safewrite(xcptfile, (char *) &inclcount, sizeof(inclcount));
  158.  
  159.       for (k = 0; k < inclcount; k++) {
  160.          if (read(inclfile, eltname, sizeof(eltname)) != sizeof(eltname))
  161.             terminate(1, "error in reading inclusion file");
  162.          for (j = 0, eptr = eltname; j < sizeof(eltname); j++, eptr++) {
  163.             if (*eptr == '\0')
  164.                *eptr = ' ';
  165.          }
  166.          if ((elttoken = find (eltname, srec, scount)) == -1) {
  167.             memset(temp, '\0', sizeof(temp));
  168.             strncpy(temp, eltname, sizeof(eltname));
  169.             strcat(temp, " is found in inclusion but no element declaration exists");
  170.             printf("%s\n", temp);
  171.          }
  172.          safewrite(xcptfile, (char *) &elttoken, sizeof(elttoken));
  173.       }
  174.    }
  175. }
  176. /* ============================================================ */
  177. static void doexcludes(srec, scount)
  178. SYMBREC srec[];
  179. int scount;
  180. {
  181.    REGISTER int j;
  182.    int count = 0, exclcount, elttoken, k;
  183.    char eltname[NAMELEN + 1], temp[128];
  184.    REGISTER char *eptr;
  185.    long position, lseek();
  186.  
  187.    /* save current position */
  188.    if ((position = lseek(xcptfile, 0L, SEEK_CUR)) == -1)
  189.       terminate(1, "error on lseek on xcptfile");
  190.    safewrite(xcptfile, (char *) &count, sizeof(count));
  191.    for (count = 0; ; count++) {
  192.       if (read(exclfile, eltname, sizeof(eltname)) != sizeof(eltname)){
  193.          safeclose(exclfile, "exclfile", EXCLFILE);
  194.          if ((position = lseek(xcptfile, position, SEEK_SET)) == -1)
  195.             terminate(1, "error on lseek on xcptfile");
  196.          safewrite(xcptfile, (char *) &count, sizeof(count));
  197.          return;
  198.       }
  199.       for (j = 0, eptr = eltname; j < sizeof(eltname); j++, eptr++) {
  200.          if (*eptr == '\0')
  201.             *eptr = ' ';
  202.       }
  203.       if ((elttoken = find (eltname, srec, scount)) == -1) {
  204.          memset(temp, '\0', sizeof(temp));
  205.          strncpy(temp, eltname, sizeof(eltname));
  206.          strcat(temp, " is found in exclusion but no element declaration exists");
  207.          printf("%s\n", temp);
  208.       }
  209.       safewrite(xcptfile, (char *) &elttoken, sizeof(elttoken));
  210.  
  211.       if(read(exclfile, (char *) &exclcount, sizeof(exclcount)) != sizeof(exclcount))
  212.          terminate(1, "error in reading exclusion file");
  213.       safewrite(xcptfile, (char *) &exclcount, sizeof(exclcount));
  214.  
  215.       for (k = 0; k < exclcount; k++) {
  216.          if (read(exclfile, eltname, sizeof(eltname)) != sizeof(eltname))
  217.             terminate(1, "error in reading exclusion file");
  218.          for (j = 0, eptr = eltname; j < sizeof(eltname); j++, eptr++) {
  219.             if (*eptr == '\0')
  220.                *eptr = ' ';
  221.          }
  222.          if ((elttoken = find (eltname, srec, scount)) == -1) {
  223.             memset(temp, '\0', sizeof(temp));
  224.             strncpy(temp, eltname, sizeof(eltname));
  225.             strcat(temp, " is found in exclusion but no element declaration exists");
  226.             printf("%s\n", temp);
  227.          }
  228.          safewrite(xcptfile, (char *) &elttoken, sizeof(elttoken));
  229.       }
  230.    }
  231. }
  232. /* ============================================================ */
  233. /*jstatic*/ void bldtokentree(srec, minrecs, count, rootgi)
  234. SYMBREC srec[];
  235. int minrecs[];
  236. int count;
  237. int rootgi;
  238. {
  239.    TREEREC trec;
  240.    DTDREC drec;
  241.    REGISTER int j, flag;
  242.    int andcount;
  243.    extern char treefname[], dtdfname[], attrfname[];
  244.    char temp[128];
  245.  
  246.    FUNCTRACE("bldtokentree");
  247.    dtdfile = safecreat(dtdfname, DTDFILE);
  248.  
  249.    treefile = safeopen(treefname, O_RDONLY, TREEFILE);
  250.    safewrite(dtdfile, (char *)&count, sizeof(count));
  251.    safewrite(dtdfile, (char *)&rootgi, sizeof(rootgi));
  252.    for (j = 0; j < count; j++) {
  253.       srec[j].Smin = minrecs[j];
  254.       srec[j].Sid = j;
  255.       safewrite(dtdfile, (char *)&srec[j], sizeof (SYMBREC));
  256.    }
  257.  
  258.    while (read(treefile, (char *)&trec, sizeof(trec)) > 0) {
  259.       if (trec.Toi == 'x')
  260.          continue;
  261.  
  262.       for (j = 0, flag = FALSE; j < NAMELEN; j++){
  263.          if (trec.Tinfo[j] == '\0')
  264.             flag = TRUE;
  265.          if (flag == TRUE)
  266.             trec.Tinfo[j] = ' ';
  267.       }
  268.       drec.Dcontreq = trec.Tcontreq;
  269.       if (trec.Tinfo[0] == ',')
  270.          drec.Dtoken = COMMA;
  271.       else if (trec.Tinfo[0] == '&') {
  272.          drec.Dtoken = AMPERSAND;
  273.       }
  274.       else if (trec.Tinfo[0] == '|')
  275.          drec.Dtoken = OR;
  276.       else if (strncmp(trec.Tinfo, "#PCDATA", 7) == 0)
  277.          drec.Dtoken = -1;
  278.       else if (strncmp(trec.Tinfo, "rCDATA", 6) == 0)
  279.          drec.Dtoken = -3;
  280.       else if (strncmp(trec.Tinfo, "cDATA", 5) == 0)
  281.          drec.Dtoken = -4;
  282.       else if (strncmp(trec.Tinfo, "eMPTY", 5) == 0)
  283.          drec.Dtoken = -5;
  284.       else if (strncmp(trec.Tinfo, "aNY", 3) == 0)
  285.          drec.Dtoken = -6;
  286.       else if ((drec.Dtoken = find(trec.Tinfo, srec, count)) == -1){
  287.          memset(temp, '\0', sizeof(temp));
  288.          strncpy(temp, trec.Tinfo, NAMELEN);
  289.          *(temp + NAMELEN) = '\0';
  290.          printf("\n\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n");
  291.          printf("%s found in content model but --\n", temp);
  292.          printf("no ELEMENT declaration for %s was found\n", temp);
  293.          if ((debug & WAITFORCR) != FALSE) {
  294.             printf("\npress ENTER to continue -> \n");
  295.             (void) getchar();
  296.          }
  297.          errflag = 1;
  298.       }
  299.       drec.Doi = trec.Toi;
  300.       safewrite(dtdfile, (char *)&drec, sizeof (drec));
  301.       if (drec.Dtoken == AMPERSAND) {
  302.          read(treefile, (char *) &andcount, sizeof(andcount));
  303.          safewrite(dtdfile, (char *) &andcount, sizeof(andcount));
  304.       }
  305.    }
  306.    closeall();
  307.    unlinkall(FALSE);
  308.    exit(errflag);
  309. }
  310. /* ============================================================ */
  311. int find(strng, srec, count)
  312. char strng[];
  313. SYMBREC srec[];
  314. register int count;
  315. {
  316.    register int j;
  317.  
  318.    FUNCTRACE("find");
  319.    for (j = 0; j <= count; j++) {
  320.       if (strncmp(strng, srec[j].Sname, NAMELEN) == 0)
  321.          return(j);
  322.    }
  323.    return(-1);
  324. }
  325.  
  326. /* ============================================================ */
  327. int find2(strng, srec, count, dupchk)
  328. char strng[];
  329. SYMBREC srec[];
  330. REGISTER int count;
  331. int dupchk;
  332. {
  333.    REGISTER int j;
  334.    static char found[MAXELTS];
  335.    char temp[128];
  336.  
  337.    FUNCTRACE("find");
  338.    for (j = 0; j <= count; j++) {
  339.       if (strncmp(strng, srec[j].Sname, NAMELEN) == 0) {
  340.          if (dupchk == OFF)
  341.             return(j);
  342.          else {
  343.             if (found[j] == 0) {
  344.                found[j] = 1;
  345.                return(j);
  346.             }
  347.             else {
  348.                strcpy(temp, "Multiple ATTLISTS exist for:  ");
  349.                strncat(temp, strng, NAMELEN);
  350.                terminate(1, temp);
  351.             }
  352.          }
  353.       }
  354.    }
  355.    return(-1);
  356. }
  357. /* ============================================================ */
  358. void ckattributes(srec, count)
  359. SYMBREC srec[];
  360. int count;
  361. {
  362.    int j, i, minusone = -1, token, namecount, attrcount,
  363.    declcode, dfltcode, grpcount, readstat, eltnum;
  364.    REGISTER int k;
  365.  
  366.    char eltnames[GRPCNT * (NAMELEN + 1)];
  367.    REGISTER char *tptr;
  368.    char grpnames[NAMELEN + 1];
  369.    char dfltval[LITLEN + 1], attrname[NAMELEN], dummy[128];
  370.  
  371.    FUNCTRACE("ckattributes");
  372.    attrtemp = safeopen(attrtname, O_RDONLY, ATTRTEMP);
  373.    attrfile = safecreat(attrfname, ATTRFILE);
  374.    if (setjmp(Xenv) != 0)
  375.       errflag = 1;
  376.    for(eltnum = 1;;eltnum++) {
  377.       readstat = read(attrtemp, (char *) &namecount, sizeof(namecount));
  378.       if (readstat != sizeof(namecount))
  379.          break;
  380.       for (tptr = eltnames, k = 0; k < namecount; k++) {
  381.          read(attrtemp, tptr, NAMELEN + 1);
  382.          /*         printf("%d.%d ATTLIST for element %s\n", eltnum, k, tptr);*/
  383.          tptr += (NAMELEN + 1);
  384.       }
  385.       for (tptr = eltnames, j = 0; j < sizeof(eltnames); tptr++, j++) {
  386.          if(*tptr == '\0')
  387.             *tptr = ' ';
  388.       }
  389.       readstat = read(attrtemp, (char *) &attrcount, sizeof(attrcount));
  390.       safewrite(attrfile, (char *) &namecount, sizeof (namecount));
  391.       for(k = 0, tptr = eltnames; k < namecount; k++) {
  392.          if((token = find2(tptr, srec, count, ON)) == -1){
  393.             strcpy(dummy, "ELEMENT ");
  394.             strncat(dummy, tptr, NAMELEN);
  395.             strcat(dummy," NOT FOUND but referenced in ATTLIST");
  396.             terminate(1, dummy);
  397.          }
  398.          safewrite(attrfile, (char *) &token, sizeof(token));
  399.          tptr += (NAMELEN + 1);
  400.       }
  401.       safewrite(attrfile, (char *) &attrcount, sizeof(attrcount));
  402.       for (i = 0; i < attrcount; i++) {
  403.          memset(attrname, '\0', sizeof(attrname));
  404.          readstat = read(attrtemp, attrname, sizeof(attrname));
  405.          safewrite(attrfile, attrname, sizeof(attrname));
  406.  
  407.          readstat = read(attrtemp, (char *) &declcode, sizeof(declcode));
  408.          safewrite(attrfile, (char *) &declcode, sizeof(declcode));
  409.          if (declcode == NOTATION) {
  410.             for(k = 0, tptr = eltnames; k < namecount; k++) {
  411.                token = find2(tptr, srec, count, OFF);
  412.                if (srec[token].Smin & (1 << 7))
  413.                   syntxerr("notation attribute for empty content");
  414.                tptr += (NAMELEN + 1);
  415.             }
  416.          }
  417.          readstat = read(attrtemp, (char *) &dfltcode, sizeof(dfltcode));
  418.          if (dfltcode == A_CONREF) {
  419.             for(k = 0, tptr = eltnames; k < namecount; k++) {
  420.                token = find2(tptr, srec, count, OFF);
  421.                if (srec[token].Smin & (1 << 7))
  422.                   syntxerr("CONREF attribute for empty content");
  423.                tptr += (NAMELEN + 1);
  424.             }
  425.          }
  426.          safewrite(attrfile, (char *) &dfltcode, sizeof(dfltcode));
  427.  
  428.          memset(dfltval, '\0', sizeof(dfltval));
  429.          readstat = read(attrtemp, dfltval, sizeof(dfltval));
  430.          safewrite(attrfile, dfltval, strlen(dfltval) + 1);
  431.  
  432.          readstat = read(attrtemp, (char *) &grpcount, sizeof(grpcount));
  433.          safewrite(attrfile, (char *) &grpcount, sizeof(grpcount));
  434.          for(j = 0; j < grpcount; j++) {
  435.             memset (grpnames, '\0', sizeof(grpnames));
  436.             readstat = read(attrtemp, grpnames, sizeof(grpnames));
  437.             if (declcode == NOTATION)
  438.                cknotation(grpnames);
  439.             for(k = 0; k < NAMELEN; k++) {
  440.                if (grpnames[k] == '\0')
  441.                   grpnames[k] = ' ';
  442.             }
  443.             safewrite(attrfile, grpnames, NAMELEN);
  444.          }
  445.       }
  446.    }
  447.    safeclose(attrtemp, "attrtemp", ATTRTEMP);
  448.    safewrite(attrfile, (char *) &minusone, sizeof(minusone));
  449.    safeclose(attrfile, "attrfile", ATTRFILE);
  450. }
  451. /* ============================================================ */
  452. void closeall()
  453. {
  454.    if (filemask & TREEFILE)
  455.       safeclose(treefile,"treefile", TREEFILE);
  456.    if (filemask & SYMBFILE)
  457.       safeclose(symbfile,"symbolfile", SYMBFILE);
  458.    if (filemask & ATTRTEMP)
  459.       safeclose(attrtemp,"attrtemp", ATTRTEMP);
  460.    if (filemask & PREFFILE)
  461.       safeclose(preffile,"preffile", PREFFILE);
  462.    if (filemask & GREFFILE)
  463.       safeclose(greffile,"greffile", GREFFILE);
  464.    if (filemask & ATTRFILE)
  465.       safeclose(attrfile,"attrfile", ATTRFILE);
  466.    if (filemask & DOCFILE)
  467.       safefclose(docfile,"docfile", DOCFILE);
  468.    if (filemask & XCPTFILE)
  469.       safeclose(xcptfile,"exception", XCPTFILE);
  470.    if (filemask & DTDFILE)
  471.       safeclose(dtdfile,"dtdfile", DTDFILE);
  472.    if (filemask & INCLFILE)
  473.       safeclose(inclfile,"inclfile", INCLFILE);
  474.    if (filemask & EXCLFILE)
  475.       safeclose(exclfile,"exclfile", EXCLFILE);
  476.    if (filemask & CMFILE)
  477.       safefclose(cmfile,"cmfile", CMFILE);
  478.  
  479.    filemask = 0;
  480. }
  481. /* ============================================================ */
  482. /* ============================================================ */
  483. void unlinkall(flag)
  484. int flag;
  485. {
  486.    (void) unlink(treefname);
  487.    (void) unlink(symbfname);
  488.    (void) unlink(attrtname);
  489.    (void) unlink(exclfname);
  490.    (void) unlink(inclfname);
  491.    if (flag == TRUE) {
  492.       (void) unlink(cmfname);
  493.       (void) unlink(preffname);
  494.       (void) unlink(greffname);
  495.       (void) unlink(attrfname);
  496.       (void) unlink(xcptfname);
  497.    }
  498. }
  499. /* ============================================================ */
  500.  
  501.  
  502.